package com.ohs.controller.before;

import com.ohs.pojo.Address;
import com.ohs.pojo.District;
import com.ohs.service.AddressService;
import com.ohs.service.DistrictService;
import com.ohs.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private DistrictService districtService;

    @Autowired
    private AddressService addressService;

    @RequestMapping("/toAddress")
    public String toAddress(){
        return "before/address";
    }

    @RequestMapping("/toAddAddress")
    public String toAddAddress(){
        return "before/addAddress";
    }

    @PostMapping("/setDefault")
    @ResponseBody
    public ResponseResult setDefault(Integer addId, HttpSession session){
        ResponseResult responseResult= this.addressService.setDefault(addId,session);
        return responseResult;
    }
    //保存地址
    @PostMapping("/save")
    @ResponseBody
    public ResponseResult save(Address address, HttpSession session){
        ResponseResult result = addressService.saveAddress(address,session);
        return result;
    }
    @PostMapping("/update")
    @ResponseBody
    public ResponseResult update(Address address, HttpSession session){
        ResponseResult result = addressService.updateAddress(address,session);
        return result;
    }

    @PostMapping("/deleteAddress")
    @ResponseBody
    public ResponseResult deleteAddress(Integer addId, HttpSession session){
        ResponseResult responseResult = this.addressService.deleteAddress(addId,session);
        return responseResult;
    }
    @RequestMapping("/getAllAddressInfo")
    @ResponseBody
    public ResponseResult getAllAddressInfo(){
       ResponseResult responseResult = this.addressService.findAllAddressInfo();
        return responseResult;
    }

    @RequestMapping("/findAllProvincesByParent")
    @ResponseBody
    public ResponseResult findAllProvincesByParent(String parent){
        List<District> districts =this.districtService.findAllProvincesByParent(parent);
        return new ResponseResult(200,districts);
    }

    @RequestMapping("/findCityByParent")
    @ResponseBody
    public ResponseResult findCityByParent(String parent){
        List<District> districtList= this.districtService.findCityByParent(parent);
        return new ResponseResult(200,districtList);
    }

    @RequestMapping("/findCountyByParent")
    @ResponseBody
    public ResponseResult findCountyByParent(String parent){
        List<District> districts=this.districtService.findCountyByParent(parent);
        return new ResponseResult(200,districts);
    }

    @RequestMapping("/toUpdateAddress")
    public String toUpdateAddress(Integer addId, Model model){
        Address address= this.addressService.findAddressOne(addId);
        model.addAttribute("address",address);
        System.out.println(address.getDetail());
        return "before/updateAddress";
    }

}
